package com.sylli.oeuf.server.object.transferable;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public /* abstract */ class BasedTO implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6996167747032195596L;
	
	public BasedTO() {
		
	}
	
	public byte[] toBytes() throws IOException {
		return toBytes(this);
	}
	
	public static byte[] toBytes(Serializable s) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos;
		oos = new ObjectOutputStream(baos);
		oos.writeObject(s);
		oos.close();
		return baos.toByteArray();
	}
	
	public static Object fromBytes(byte[] bytes) throws IOException, ClassNotFoundException {
		ByteArrayInputStream bais;
		ObjectInputStream ois;
		bais = new ByteArrayInputStream(bytes);
		ois = new ObjectInputStream(bais);
		return ois.readObject();
	}
}
